package com.jeomo.masterdata.provider.web.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jeomo.base.provider.controller.BaseController;
import com.jeomo.common.core.exception.BusinessException;
import com.jeomo.common.core.result.CommonResult;
import com.jeomo.common.core.util.AliOssUtils;
import com.jeomo.common.core.util.BeanCopyUtil;
import com.jeomo.masterdata.provider.request.MemberLevelRequest;
import com.jeomo.masterdata.provider.web.vo.LevelVo;
import com.jeomo.masterdata.starter.entity.Level;
import com.jeomo.masterdata.starter.service.ILevelService;
import com.jeomo.shiro.util.LoginSessionUtil;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * <h3>jeomo</h3>
 * <p>等级controller</p>
 *
 * @author : 崔贺
 * @date : 2020-08-28 13:56
 **/
@RequestMapping("/masterData/level")
@RestController
public class LevelController extends BaseController {

    @Resource
    private ILevelService levelService;

    @Resource
    private AliOssUtils aliOssUtils;


    @PostMapping
    @ApiOperation("创建等级")
    public CommonResult<Integer> create(@RequestBody @Validated MemberLevelRequest createLevelRequest) {
        Level level = BeanCopyUtil.copyProperties(createLevelRequest, Level.class);
        return CommonResult.success(levelService.create(level));
    }

    @RequestMapping("/upload")
    public CommonResult<String> upload(@RequestParam MultipartFile file) throws IOException {
        CommonResult<String> upload = aliOssUtils.upload(file.getInputStream(), file.getOriginalFilename());
        return CommonResult.success(upload.getData());
    }

    @DeleteMapping("/filePath")
    public CommonResult<String> deleteFile(@RequestParam String path) {
        Boolean aBoolean = aliOssUtils.delete(path);
        if (aBoolean) {
            return CommonResult.success("删除成功");
        }
        return CommonResult.failure("删除失败，没有找到此文件");
    }

    @DeleteMapping("/{code}")
    @ApiOperation("删除等级")
    public CommonResult<Boolean> remove(@PathVariable("code") String code) {
        return CommonResult.success(levelService.removeById(code));
    }

    @PutMapping
    @ApiOperation("修改等级")
    public CommonResult<Boolean> update(@RequestBody @Validated MemberLevelRequest memberLevelRequest) {
        if (memberLevelRequest.getUpLevelDailyAmount() > memberLevelRequest.getUpLevelSumAmount()) {
            throw new BusinessException("升级规则的单日累计消费金额必须小于等于总累计消费金额!");
        }
        return CommonResult.success(levelService.updateById(BeanCopyUtil.copyProperties(memberLevelRequest, Level.class)));
    }

    @GetMapping("/{code}")
    @ApiOperation("根据品牌号查询品牌实体")
    public CommonResult<LevelVo> queryByCode(@PathVariable("code") String code) {
        Level level = levelService.getById(code);
        return CommonResult.success(BeanCopyUtil.copyProperties(level, LevelVo.class));
    }

    @GetMapping("/all")
    @ApiOperation("获取所有Level(不包括停用)")
    public CommonResult<List<LevelVo>> all() {
        List<Level> list = levelService.allLevel();
        return CommonResult.success(BeanCopyUtil.copyListProperties(list, LevelVo::new));
    }

    @GetMapping("/allList")
    @ApiOperation("获取所有Level(包括停用)")
    public CommonResult<List<LevelVo>> allList() {
        List<Level> list = levelService.allLevelList();
        return CommonResult.success(BeanCopyUtil.copyListProperties(list, LevelVo::new));
    }

    @GetMapping("/levelCodes")
    @ApiOperation("获取等级编码")
    public CommonResult<List<String>> getLevelCodes() {
        List<String> levelCodesList = Arrays.asList(Level.LEVEL_CODES_ARR);
        List<String> levelCodesArrList = new ArrayList<>(levelCodesList);
        QueryWrapper<Level> wrapper = new QueryWrapper<>();
        wrapper.eq("org_code", LoginSessionUtil.getCurrentLoginInfo().getCurrentOrgCode());
        List<Level> levels = levelService.list(wrapper);
        for (Level level : levels) {
            String levelCode = level.getLevelCode();
            if (levelCodesArrList.contains(levelCode)) {
                levelCodesArrList.remove(levelCode);
            }
        }
        return CommonResult.success(levelCodesArrList);
    }
}
